package com.tjec.project.esb.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.tjec.project.esb.domain.EsbReqHead;
import com.tjec.project.esb.service.IPushToPortalUserInfoService;
import com.tjec.project.mdmSynchronous.api.HttpLocalUtils;
import com.tjec.project.system.domain.ToPortalUserInfo;

@Service
@Configuration
public class PushToPortalUserInfoServiceImpl implements IPushToPortalUserInfoService{
	
	private static final Logger log = LoggerFactory.getLogger(PushToPortalUserInfoServiceImpl.class);
	
	@Value("${ESB.systemName}")
	private String systemName;
	
	@Value("${PORTAL.url}")
	private String portalUrl;
	
	@Value("${PORTAL.sourceSystem}")
	private String sourceSystem;
	
	@Value("${PORTAL.targetSystem}")
	private String targetSystem;
	
	@Value("${PORTAL.serviceName}")
	private String serviceName;
	
	@Override
	public void sendUserInfo(ToPortalUserInfo toPortalUserInfo) {
		
		Map<String,Object> reqMapBody = new HashMap<String,Object>();
		
		reqMapBody.put("systemName", systemName);
		reqMapBody.put("updateTime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
		reqMapBody.put("userNo", toPortalUserInfo.getUserNo()==null?"":toPortalUserInfo.getUserNo());
		reqMapBody.put("loginName", toPortalUserInfo.getLoginName()==null?"":toPortalUserInfo.getLoginName());
		reqMapBody.put("competentDept", toPortalUserInfo.getCompetentOrg()==null?"":toPortalUserInfo.getCompetentOrg());
		reqMapBody.put("competentOrg", "");
		reqMapBody.put("loginDate", toPortalUserInfo.getLoginDate()==null?"":toPortalUserInfo.getLoginDate());
		reqMapBody.put("flag", toPortalUserInfo.getFlag()==null?"":toPortalUserInfo.getFlag());
		reqMapBody.put("userStatus", toPortalUserInfo.getUserStatus()==null?"":toPortalUserInfo.getUserStatus());
		reqMapBody.put("userName", toPortalUserInfo.getUserName()==null?"":toPortalUserInfo.getUserName());
		
		
		String reqData = JSONArray.toJSON(reqMapBody).toString();
		log.info("推送门户参数： "+reqData);
		
		String result = HttpLocalUtils.doPostToESB(portalUrl, reqData, new EsbReqHead(sourceSystem, targetSystem, UUID.randomUUID().toString(), serviceName));
		
		log.info("门户返回结果： "+result);
		
	}

	/*
	 public static void main(String[] args) {
		Map<String,Object> reqMapHead = new HashMap<String,Object>();
		Map<String,Object> reqMapBody = new HashMap<String,Object>();
		ToPortalUserInfo toPortalUserInfo = new ToPortalUserInfo();
		reqMapBody.put("systemName", "EC");
		reqMapBody.put("updateTime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
		reqMapBody.put("userNo", toPortalUserInfo.getUserNo()==null?"":toPortalUserInfo.getUserNo());
		reqMapBody.put("loginName", toPortalUserInfo.getLoginName()==null?"":toPortalUserInfo.getLoginName());
		reqMapBody.put("competentDept", "同济咨询");
		reqMapBody.put("competentOrg", toPortalUserInfo.getCompetentOrg()==null?"":toPortalUserInfo.getCompetentOrg());
		reqMapBody.put("loginDate", toPortalUserInfo.getLoginDate()==null?"":toPortalUserInfo.getLoginDate());
		reqMapBody.put("flag", toPortalUserInfo.getFlag()==null?"":toPortalUserInfo.getFlag());
		reqMapBody.put("userStatus", toPortalUserInfo.getUserStatus()==null?"":toPortalUserInfo.getUserStatus());
		
		reqMapHead.put("EsbReqData",reqMapBody);
		System.out.println(JSONArray.toJSON(reqMapHead).toString());
	}*/
	
}
